import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import '/plugin/dio_new/dio_new.dart';
import 'package:get/get.dart';

import '/utils/api.dart';

class SelectRadio extends StatefulWidget {
  final String title;
  final bool required;
  final ValueChanged<String>? onChange;
  final bool isDict;
  final String? dictCode;
  SelectRadio({
    Key? key,
    required this.title,
    required this.required,
    this.onChange,
    this.dictCode,
    required this.isDict,
  }) : super(key: key);

  @override
  _SelectRadioState createState() => _SelectRadioState();
}

class _SelectRadioState extends State<SelectRadio> {
  late Widget labelText;
  late String selectString = '请选择';
  late HttpClient dio;
  late List dataList = [];
  late List<Widget> selectList = [];
  int initialItem = 0;
  Future<void> _initDict() async {
    if (widget.isDict && widget.dictCode != null) {
      HttpResponse appResponse = await dio.get(
          '${Api.getDictItems}${widget.dictCode}',
          queryParameters: {"dictCode": widget.dictCode});
      if (appResponse.ok) {
        dataList = appResponse.data;

        if (dataList.length > 0) {
          dataList.forEach((e) => {selectList.add(Text(e['text']))});
        }
        print(selectList);
      }
    }
  }

  void _showCupertinoPicker(BuildContext cxt) {
    setState(() {
      selectString = dataList[initialItem]['text'];
      widget.onChange?.call(dataList[initialItem]['value']);
    });
    final picker = CupertinoPicker(
      backgroundColor: Colors.white, //选择器背景色
      itemExtent: 30, //item的高度
      scrollController:
          new FixedExtentScrollController(initialItem: initialItem),
      onSelectedItemChanged: (index) {
        //选中item的位置索引
        setState(() {
          initialItem = index;
          selectString = dataList[index]['text'];
          widget.onChange?.call(dataList[index]['value']);
        });
      },
      children: selectList,
    );

    showCupertinoModalPopup(
      context: cxt,
      builder: (cxt) {
        return Container(
          height: 200,
          child: picker,
        );
      },
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    dio = Get.find<HttpClient>();
    _initDict();
    if (widget.required) {
      labelText = Row(
        children: [
          Text(
            '* ',
            style: TextStyle(color: Colors.red),
          ),
          Text(widget.title),
        ],
      );
    } else {
      labelText = Text(widget.title);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          InkWell(
            onTap: () => _showCupertinoPicker(context),
            child: Container(
              color: Colors.white,
              child: ListTile(
                title: labelText,
                trailing: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Container(
                      width: 150,
                      padding: EdgeInsets.all(5),
                      child: Text(
                        selectString,
                        overflow: TextOverflow.ellipsis,
                        maxLines: 1,
                        textAlign: TextAlign.right,
                      ),
                    ),
                    selectString == '请选择'
                        ? Icon(Icons.keyboard_arrow_right)
                        : Text(''),
                  ],
                ),
              ),
            ),
          ),
          Divider(height: 1, indent: 20),
        ],
      ),
    );
  }
}
